import { defineStore } from 'pinia'
import { ref } from 'vue'
import { request } from '../utils/request'

interface Certification {
  name: string
  image: string
  order: number
}

interface ProcessStep {
  step: number
  title_en: string
  title_es: string
  title_ja: string
  description_en: string
  description_es: string
  description_ja: string
  icon: string
}

interface HomepageSettings {
  homepage_hero_background?: string
  homepage_logo?: string
  homepage_company_name_en?: string
  homepage_company_name_es?: string
  homepage_company_name_ja?: string
  homepage_certifications?: Certification[]
  homepage_products_title_en?: string
  homepage_products_title_es?: string
  homepage_products_title_ja?: string
  homepage_about_title_en?: string
  homepage_about_title_es?: string
  homepage_about_title_ja?: string
  homepage_about_content_en?: string
  homepage_about_content_es?: string
  homepage_about_content_ja?: string
  homepage_about_image?: string
  homepage_video_url?: string
  homepage_process_steps?: ProcessStep[]
  [key: string]: any // 支持动态产品配置
}

export const useHomepageStore = defineStore('homepage', () => {
  const settings = ref<HomepageSettings>({})
  const loading = ref(false)
  const error = ref<string | null>(null)
  
  // 获取首页配置
  const fetchSettings = async () => {
    loading.value = true
    error.value = null
    
    try {
      const response = await request.get('/settings/public')
      if (response.success) {
        settings.value = response.data
      } else {
        error.value = response.message || '获取配置失败'
      }
    } catch (err: any) {
      error.value = err.message || '网络错误'
      console.error('获取首页配置失败:', err)
    } finally {
      loading.value = false
    }
  }
  
  // 根据语言获取公司名称
  const getCompanyName = (lang: string) => {
    const langMap: Record<string, keyof HomepageSettings> = {
      'zh-CN': 'homepage_company_name_zh',
      'en': 'homepage_company_name_en',
      'es': 'homepage_company_name_es',
      'ja': 'homepage_company_name_ja'
    }
    
    const key = langMap[lang] || 'homepage_company_name_en'
    return settings.value[key] || 'China Sea Bass Capital Zhuhai'
  }
  
  // 根据语言获取产品区域标题
  const getProductsTitle = (lang: string) => {
    const langMap: Record<string, keyof HomepageSettings> = {
      'zh-CN': 'homepage_products_title_zh',
      'en': 'homepage_products_title_en',
      'es': 'homepage_products_title_es',
      'ja': 'homepage_products_title_ja'
    }
    
    const key = langMap[lang] || 'homepage_products_title_en'
    return settings.value[key] || 'Our Products'
  }
  
  // 根据语言获取产品名称
  const getProductName = (index: number, lang: string) => {
    const langMap: Record<string, string> = {
      'zh-CN': 'zh',
      'en': 'en',
      'es': 'es',
      'ja': 'ja'
    }
    
    const langSuffix = langMap[lang] || 'en'
    const key = `homepage_product_${index}_name_${langSuffix}`
    return settings.value[key] || ''
  }
  
  // 获取产品图片
  const getProductImage = (index: number) => {
    const key = `homepage_product_${index}_image`
    return settings.value[key] || ''
  }
  
  // 获取所有产品数据
  const getProducts = (lang: string) => {
    const products = []
    for (let i = 1; i <= 6; i++) {
      const image = getProductImage(i)
      const name = getProductName(i, lang)
      if (image || name) {
        products.push({
          index: i,
          image,
          name
        })
      }
    }
    return products
  }
  
  // 获取质量认证图标
  const getCertifications = () => {
    return settings.value.homepage_certifications || []
  }

  // 根据语言获取About Us标题
  const getAboutTitle = (lang: string) => {
    const langMap: Record<string, keyof HomepageSettings> = {
      'zh-CN': 'homepage_about_title_zh',
      'en': 'homepage_about_title_en',
      'es': 'homepage_about_title_es',
      'ja': 'homepage_about_title_ja'
    }
    
    const key = langMap[lang] || 'homepage_about_title_en'
    return settings.value[key] || 'ABOUT US'
  }

  // 根据语言获取About Us内容
  const getAboutContent = (lang: string) => {
    const langMap: Record<string, keyof HomepageSettings> = {
      'zh-CN': 'homepage_about_content_zh',
      'en': 'homepage_about_content_en',
      'es': 'homepage_about_content_es',
      'ja': 'homepage_about_content_ja'
    }
    
    const key = langMap[lang] || 'homepage_about_content_en'
    return settings.value[key] || ''
  }

  // 获取About Us图片
  const getAboutImage = () => {
    return settings.value.homepage_about_image || ''
  }

  // 获取视频URL
  const getVideoUrl = () => {
    return settings.value.homepage_video_url || ''
  }

  // 获取流程步骤
  const getProcessSteps = () => {
    return settings.value.homepage_process_steps || []
  }

  // 根据语言获取步骤标题
  const getProcessStepTitle = (step: ProcessStep, lang: string) => {
    const langMap: Record<string, keyof ProcessStep> = {
      'zh-CN': 'title_zh' as keyof ProcessStep,
      'en': 'title_en',
      'es': 'title_es', 
      'ja': 'title_ja'
    }
    
    const key = langMap[lang] || 'title_en'
    return step[key] || step.title_en
  }

  // 根据语言获取步骤描述
  const getProcessStepDescription = (step: ProcessStep, lang: string) => {
    const langMap: Record<string, keyof ProcessStep> = {
      'zh-CN': 'description_zh' as keyof ProcessStep,
      'en': 'description_en',
      'es': 'description_es',
      'ja': 'description_ja'
    }
    
    const key = langMap[lang] || 'description_en'
    return step[key] || step.description_en
  }
  
  return {
    settings,
    loading,
    error,
    fetchSettings,
    getCompanyName,
    getProductsTitle,
    getProductName,
    getProductImage,
    getProducts,
    getCertifications,
    getAboutTitle,
    getAboutContent,
    getAboutImage,
    getVideoUrl,
    getProcessSteps,
    getProcessStepTitle,
    getProcessStepDescription
  }
})